package com.example.springbootblog.mapper;


import org.springframework.util.DigestUtils;

import java.util.UUID;

public class SecurityUtiles {
    /**
     * 加密
     * @param password 明文加密
     * @return 盐值+密码
     */
    public static String encry(String password) {
        // 盐值 去除-
        String salt = UUID.randomUUID().toString().replace("-","");
        System.out.println("salt: " + salt);
        // 加密 盐值+明文
        String securityPassword = DigestUtils.md5DigestAsHex((salt + password).getBytes());
        System.out.println("securityPassword: " + securityPassword);
        // 返回盐值+密文
        return salt + securityPassword;
    }

    /**
     * 校验
     * @return
     */
    public static boolean verify(String inputPassword, String sqlPasswrod) {
        // 拿到盐值
        String salt = sqlPasswrod.substring(0,32);
        // 加密, 盐值 和 明文
        String securityPassword = DigestUtils.md5DigestAsHex((salt + inputPassword).getBytes());

        // 校验 数据库的密文 和 新密文是否一致
        return (salt + securityPassword).equals(sqlPasswrod);
    }
    public static void main(String[] args) {
        String str = encry("123456");
        System.out.println(str);
        String str2 = "7fcd3cd73bff43549aa7c973aa7a471d363b6c1b3f811e384f06c57cec1ef27d";
        String str3 = "d6c6f7111b23466eb84882441bc724129cf128d5d3f4b4987060a50a23af4062";
        boolean result = verify("123456","d6c6f7111b23466eb84882441bc724129cf128d5d3f4b4987060a50a23af4062");
        System.out.println(result);
    }
}
